package com.hiultra.archive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.domain.ArchiveDes;
import com.hiultra.archive.domain.ArchiveDesTask;
import com.hiultra.archive.domain.ArchiveDesUser;
import com.hiultra.archive.domain.RoomArchive;
import com.hiultra.archive.mapper.ArchiveDesMapper;
import com.hiultra.archive.mapper.ArchiveDesTaskMapper;
import com.hiultra.archive.mapper.ArchiveDesUserMapper;
import com.hiultra.archive.mapper.RoomArchiveMapper;
import com.hiultra.archive.service.IArchiveDesUserService;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销毁用户Service业务层处理
 *
 * @author paidaxing
 * @date 2024-08-03
 */
@Service
public class ArchiveDesUserServiceImpl implements IArchiveDesUserService {
    @Autowired
    private ArchiveDesUserMapper archiveDesUserMapper;
    @Autowired
    private RoomArchiveMapper roomArchiveMapper;
    @Autowired
    private ArchiveDesTaskMapper archiveDesTaskMapper;
    @Autowired
    private ArchiveDesMapper archiveDesMapper;

    /**
     * 查询销毁用户
     *
     * @param id 销毁用户主键
     * @return 销毁用户
     */
    @Override
    public ArchiveDesUser selectArchiveDesUserById(String id) {
        return archiveDesUserMapper.selectArchiveDesUserById(id);
    }

    /**
     * 查询销毁用户列表
     *
     * @param archiveDesUser 销毁用户
     * @return 销毁用户
     */
    @Override
    public List<ArchiveDesUser> selectArchiveDesUserList(ArchiveDesUser archiveDesUser) {
        return archiveDesUserMapper.selectArchiveDesUserList(archiveDesUser);
    }

    /**
     * 新增销毁用户
     *
     * @param archiveDesUser 销毁用户
     * @return 结果
     */
    @Override
    public int insertArchiveDesUser(ArchiveDesUser archiveDesUser) {
        return archiveDesUserMapper.insertArchiveDesUser(archiveDesUser);
    }

    /**
     * 修改销毁用户
     *
     * @param archiveDesUser 销毁用户
     * @return 结果
     */
    @Override
    public int updateArchiveDesUser(ArchiveDesUser archiveDesUser) {
        return archiveDesUserMapper.updateArchiveDesUser(archiveDesUser);
    }

    /**
     * 批量删除销毁用户
     *
     * @param ids 需要删除的销毁用户主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDesUserByIds(String[] ids) {
        return archiveDesUserMapper.deleteArchiveDesUserByIds(ids);
    }

    /**
     * 批量修改销毁用户状态
     *
     * @param ids 需要删除的销毁用户主键集合
     * @return
     */
    @Override
    @Transactional
    public int updateArchiveDesUserTaskStatus(String desStatus, String[] ids) {
        int i = archiveDesUserMapper.updateArchiveDesUserTaskStatus(desStatus, ids);
        String taskId = null;
        for (String id : ids) {
            ArchiveDesUser archiveDesUser = archiveDesUserMapper.selectArchiveDesUserById(id);
            if (ObjectUtil.isNotNull(archiveDesUser)) {
                taskId = archiveDesUser.getTaskId();
                //根据用户删除相关档案信息
                RoomArchive roomArchive = new RoomArchive();
                List<RoomArchive> roomArchiveList = roomArchiveMapper.selectRoomArchiveList(roomArchive);
                if (roomArchiveList.size() > 0) {
                    for (RoomArchive room : roomArchiveList) {
                        roomArchiveMapper.deleteRoomArchiveById(room.getId());
                    }
                }
            } else {
                throw new ServiceException("未查询到用户信息，请检查重试");
            }

        }

        //查询是否都销毁完成
        int desNum = 0;
        String finalTaskId = taskId;
        List<ArchiveDesUser> archiveDesUserListAll = archiveDesUserMapper.selectArchiveDesUserList(new ArchiveDesUser() {{
            setTaskId(finalTaskId);
        }});
        List<ArchiveDes> archiveDesListAll = archiveDesMapper.selectArchiveDesList(new ArchiveDes() {{
            setTaskId(finalTaskId);
        }});
        for (ArchiveDesUser archiveDesUser : archiveDesUserListAll) {
            if (BusinessConstants.DES_STATUS.equals(archiveDesUser.getDesStatus())) {
                desNum++;
                break;
            }
        }
        for (ArchiveDes archiveDes : archiveDesListAll) {
            if (BusinessConstants.DES_STATUS.equals(archiveDes.getDesStatus())) {
                desNum++;
                break;
            }
        }
        if (desNum <= 0) {
            ArchiveDesTask archiveDesTask = new ArchiveDesTask();
            archiveDesTask.setTaskId(finalTaskId);
            archiveDesTask.setStatus(BusinessConstants.END_DES_STATUS);
            archiveDesTaskMapper.updateArchiveDesTask(archiveDesTask);
        }

        return i;
    }

    /**
     * 删除销毁用户信息
     *
     * @param id 销毁用户主键
     * @return 结果
     */
    @Override
    public int deleteArchiveDesUserById(String id) {
        return archiveDesUserMapper.deleteArchiveDesUserById(id);
    }

    /**
     * 根据任务id查询销毁用户详情
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ArchiveDesUser> selectArchiveDesUserByTaskId(String taskId) {
        return archiveDesUserMapper.selectArchiveDesUserByTaskId(taskId);
    }

}
